home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: Passing multidimensional arrays to functions in C
- Date: 24 Jan 1996 04:00:37 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Jan23210037@qcd.lanl.gov>
- References: <vijay-2301961215460001@vijay.chinalake.navy.mil>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: vijay@nawc690.chinalake.navy.mil's message of Tue, 23 Jan 1996 20:13:31 GMT
-
- --text follows this line--
- In article <vijay-2301961215460001@vijay.chinalake.navy.mil>
- vijay@nawc690.chinalake.navy.mil (Jim Vijay) writes:
- <snip>
- The C FAQ (Q's 6.16 thru 6.20) explains how to pass 2D arrays to functions
- in C. None of the methods shown is as simple or general as languages such
- as (gasp!) FORTRAN.
-
- Different languages are suited for different things, and details like
- this often result from subtle differences in viewpoints. I use both
- Fortran and C, and am sometimes irritated by small inconveniences like
- this.
-
- While realizing that I'm inviting flames, and realizing this topic
- may have been discussed to death in the past (but not in the FAQ),
-
- I remember having participated in a similar discussion once in the
- past: and remember having seen it more than that one occassion. It is
- a fit topic for discussion, probably even in comp.std.c.
-
- let me ask:
-
- What would break (or even be damaged) in C if the language allowed
- one to pass the width of a 2D array (in general, n-1 dimensions of
- an n dim array) to a function? It should be easy for a compiler to
- emit code that dynamically depends on these n-1 dimensions.
- FORTRAN (IV?) at the time of C's birth could do it!
-
- Please note that I'm not asking how the C language defines and uses multi
- dimensional array parameters to functions. I would like to know _why_ the
- current mechanism was chosen - initially (by K&R?) and later by the ANSI
- C committees.
-
- What follows is completely my personal opinion. My knowledge of
- history is poor, so take everything with a grain of salt. (I know I
- should not violate my own rule of not posting if one does not know:
- especially since I know I am getting careless as shown by a lot of my
- recent postings; I hope this disclaimer warns you enough.)
-
- Originally C was designed in the spirit that it would be a portable
- syntax for writing code close to machine language: sort of an high
- level assembly language. No assembly language at that time had the
- concept of an array: an array therefore was not a basic type in
- C. Assembly languages provided means of allocating blocks of memory
- ... an array declaration in C originally did precisely that. As a
- result array types are somewhat special: there are no array values (or
- rvalues), any attempt to use them as an rvalue resulted in a pointer
- to the allocated block of memory being used instead. As you could
- never pass an array in the first place, how could you hope to specify
- the bounds of the array? It may be that this bias towards arrays as
- allocation units as opposed to true types contributed to
- multidimensional arrays not being treated specially.
-
- Today, the rules are somewhat better. Array rvalues are still
- absolutely useless: in most uses arrays still become pointers. But,
- today, because of the better type checking rules and non-equivalence
- of all pointer types (which were probably never specified equivalent,
- but often treated that way), arrays are first class types (or at least
- one and half class :-). But allowing array sizes to be variables is
- still a problem.
-
- 1) Currently, according to the ANSI standard, a object allocated `on
- the stack' has little runtime penalty. The compiler
- always knows the exact amount of space it needs for all its local
- variables etc. (excluding of course the heap allocation through
- malloc etc.) Allowing variable sized arrays as a type would mean
- a possible run time allocation. Allowing variable sized arrays
- _only_ in parameter definition would alleviate this
- problem. (FORTRAN chose this rule upto FORTRAN 77)
-
- 2) The existence of that type (even if only in parameter
- declarations), would imply changes in the semantics of
- sizeof. Currently sizeof returns a compile time constant and depeds
- on the type of expression, _no_ part of which is evaluated.
-
- 3) Allowing the generality of FORTRAN would also imply that the bounds
- declarations be allowed to be forward declarations. This creates
- slight parsing difficulties, and needs resolution of a few
- ambiguities.
-
- Having said all this, do I believe it would be useful? Certainly! This
- is onw extension which I use a lot, even though ultimately it is
- nothing but syntactic sugar. But then, what isn't?
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-